package com.example.testdemo.recyclerview_gallery_load;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewConfigurationCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * 自定义滑动layout
 * Created by zhangwenxiang on 2018/8/21.
 */

public class MyScrollLayout extends LinearLayout{
    private Scroller mScroller;
    //判定为拖动的最小移动像素数
    private int mTouchSlop;
    //手机按下时的屏幕坐标
    private float mYDown;
    //手机当时所处的屏幕坐标
    private float mYMove;
    //上次触发ACTION_MOVE事件时的屏幕坐标
    private float mYLastMove;

    private OnVerScrollListener listener;

    public interface OnVerScrollListener{
        void onVerScroll();
    }

    public void setOnVerScrollListener(OnVerScrollListener listener){
        this.listener = listener;
    }

    public MyScrollLayout(Context context) {
        super(context);
        init(context);
    }

    public MyScrollLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public MyScrollLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        mScroller = new Scroller(context);
        ViewConfiguration configuration = ViewConfiguration.get(context);
        // 获取TouchSlop值
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    }

    private boolean intercept;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        intercept = super.onInterceptTouchEvent(e);


        switch (e.getAction()){
            case MotionEvent.ACTION_DOWN:
                mYDown = e.getRawY();
                mYLastMove = mYDown;
                break;
            case MotionEvent.ACTION_MOVE:
                mYMove = e.getRawY();
                float diff = Math.abs(mYMove - mYDown);
                // 滑动距离是否 >触摸阈值  && 向下滑动
                if(diff > mTouchSlop && mYMove > mYDown){// && Math.abs(x- downX )< mTouchSlop
//                    Log.i("GalleryRecycler", " onInterceptTouchEvent move");
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onInterceptTouchEvent(e);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                mYMove = event.getRawY();
                int scrolledY = (int) (mYLastMove - mYMove);
                scrollBy(0, scrolledY);
                mYLastMove = mYMove;
                break;
            case MotionEvent.ACTION_UP:
                // 当手指抬起时，根据当前的滚动值来判定应该滚动到哪个子控件的界面
//                int targetIndex = (getScrollY() + getHeight() / 2) / getHeight();
//                int dy = targetIndex * getHeight() - getScrollY();
                // 调用startScroll()方法来初始化滚动数据并刷新界面
//                Log.i("MyScrollLayout", "getScrollY()="+getScrollY());

                //滑动超出控件高度1/3 视为翻页,调用startScroll()方法来初始化滚动数据并刷新界面
                if(Math.abs(getScrollY()) > getHeight()/3){
                    mScroller.startScroll(0, getScrollY(), 0, Math.abs(getScrollY()) -  getHeight(), 500);
                }else{
                    mScroller.startScroll(0, getScrollY(), 0, Math.abs(getScrollY()), 500);
                }
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
            //滑动完成 事件回调
            if(!isHidden && getHeight() == Math.abs(mScroller.getCurrY()) && mScroller.isFinished()){//getHeight() == Math.abs(mScroller.getCurrY())
//                Log.i("MyScrollLayout", "getcurry"+mScroller.getCurrY()+ "height"+ getHeight()+" isHidden="+isHidden);
                isHidden = true;
                if(listener != null){
                    listener.onVerScroll();
                }
            }
        }

    }

    //是否隐藏
    private boolean isHidden;

    /**
     * 回复到之前位置(重新显示)
     */
    public void reVisivle(){
        scrollTo(mScroller.getCurrX(), 0);
        invalidate();
        isHidden = false;
    }
}
